package com.example.administrator.myapplication.kotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Toast
import com.example.administrator.myapplication.R
import kotlinx.android.synthetic.main.activity_kotlin.*

class KotlinActivity : AppCompatActivity() {
    //不可变常量
    val TAG = "Kotlin1"
    val a: Int = 5
    //可变 变量
    var b = 6
    var d = null
    //字符串模板
    var c = 8
    var s = "c is $a"
    var s2 = "${s.replace("is", "was")},but now is $a"

    //数组
    var sz = arrayOf("标题一", "标题二", "标题三")
    //列表
    var lb= arrayListOf<String>()
    //map
    var map= mapOf<String,Any>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
        tv_test.text = "哈哈"
        tv_test.setOnClickListener({ Toast.makeText(this, tv_test.text.toString(), Toast.LENGTH_LONG).show() })
        var sum = sum(4, 5)
        Log.d(TAG, sum.toString())
        vars(1, 2, 3, 4, 5, 6)
        //lambda 函数表达式
        var sumLambd: (Int, Int) -> Int = { x, y -> x + y }
        println(sumLambd(5, 9))
        when(b){
            3 -> println(3)
            4 -> println(4)

        }
        for (i in sz.indices){
            println(sz[i])
        }
        var person=Person("小明",14,160)
//        person.age=12
//        person.height=175
        println(person.toString())
        var apple=Apple()
        apple.eat()
        var juzi=object : Fruits(){
            override fun eat() {
              println("eat orange")
            }
        }
        juzi.eat()
    }

    //普通函数
    private fun sum(a: Int, b: Int): Int {
        var c = a + b
        return c
    }

    //可变长参数
    private fun vars(vararg v: Int) {
        for (vt in v) {
            println(vt)
        }
    }

}
